/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.acc.softmgr.business.model.SoftmgrSubSystem;
import com.acc.softmgr.business.model.SoftmgrSubSystemExample;
import com.acc.softmgr.business.model.SoftmgrSystem;
import com.acc.softmgr.business.model.SoftmgrSystemExample;
import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.service.ISoftmgrProjectService;
import com.acc.softmgr.business.service.ISoftmgrSubSystemService;
import com.acc.softmgr.business.service.ISoftmgrSystemService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.date.DateBase;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.DataGridObject;
import com.acc.softmgr.common.web.ResponseObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softSubSystem")
public class SoftmgrSubSystemController {

	@Autowired
	private ISoftmgrProjectService softmgrProjectService;

	@Autowired
	private ISoftmgrSystemService softmgrSystemService;

	@Autowired
	private ISoftmgrSubSystemService softmgrSubSystemService;

	@RequestMapping(value = "toSoftSubSystem", method = RequestMethod.GET)
	public String toSoftSubSystem() {
		return "softSubSystem/softSubSystem";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftSubSystem", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrSubSystem> searchSoftSubSystem(@Validated String searchProjectId,
			@Validated String searchSystemId, @Validated String searchSubSystemName, Integer page, Integer rows,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		List<SoftmgrSystem> listSystem = null;
		List<SoftmgrSubSystem> listSubSystem = null;

		if (StringUtil.isNotEmpty(searchProjectId)) {
			listSystem = getSoftmgrSystem(Integer.valueOf(searchProjectId));
			if (StringUtil.isNotEmpty(searchSystemId)) {
				listSubSystem = getSoftmgrSubSystem(searchSystemId, searchSubSystemName, page, rows);
			} else {
				listSubSystem = getSoftmgrSubSystem(listSystem, searchSubSystemName, page, rows);
			}

		} else {
			if (StringUtil.isNotEmpty(searchSystemId)) {
				listSubSystem = getSoftmgrSubSystem(searchSystemId, searchSubSystemName, page, rows);
			} else {
				listSubSystem = getSoftmgrSubSystem(searchSystemId, searchSubSystemName, page, rows);
			}
		}

		DataGridObject<SoftmgrSubSystem> responseObject = new DataGridObject<SoftmgrSubSystem>();
		for (SoftmgrSubSystem softmgrSubSystem : listSubSystem) {
			SoftmgrSystem softmgrSystem = softmgrSystemService.selectByPrimaryKey(softmgrSubSystem.getSystem_id());
			softmgrSubSystem.setCreateTimestring(
					DateBase.formatDate(softmgrSubSystem.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
			softmgrSubSystem.setSystemName(softmgrSystem.getSystem_name());
			softmgrSubSystem.setProjectName(
					softmgrProjectService.selectByPrimaryKey(softmgrSystem.getProject_id()).getProject_name());

		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(listSubSystem);
		return responseObject;
	}

	/**
	 * 新增子系统
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftSubSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSoftSubSystem(@Validated String project_id, String system_id, String sub_system_name,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(project_id) || StringUtil.isEmpty(system_id) || StringUtil.isEmpty(sub_system_name)) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");

		SoftmgrSubSystem SoftmgrSubSystem = new SoftmgrSubSystem();
		SoftmgrSubSystem.setCreatetime(new Date());
		SoftmgrSubSystem.setCreateby(currentUser.getUsername());
		SoftmgrSubSystem.setSystem_id(Integer.valueOf(system_id));
		SoftmgrSubSystem.setSub_system_name(sub_system_name);
		SoftmgrSubSystem.setDeleted(DeleteStatus.FALSE);
		// -------------------
		boolean exitSubSystem = exitSoftmgrSubSystem(system_id, sub_system_name);

		if (exitSubSystem) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrSubSystemService.insert(SoftmgrSubSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改子系统
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftSubSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSoftSubSystem(@Validated Integer id, String system_id, String sub_system_name,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(system_id) || StringUtil.isEmpty(sub_system_name) || id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSubSystem SoftmgrSubSystem = softmgrSubSystemService.selectByPrimaryKey(id);
		boolean exitSubSystem = exitSoftmgrSubSystem(system_id, sub_system_name);
		if (exitSubSystem) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		SoftmgrSubSystem.setSystem_id(Integer.valueOf(system_id));
		SoftmgrSubSystem.setSub_system_name(sub_system_name);
		softmgrSubSystemService.updateByPrimaryKey(SoftmgrSubSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftSubSystem", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchSoftSubSystem(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSubSystem SoftmgrSubSystem = softmgrSubSystemService.selectByPrimaryKey(id);
		SoftmgrSubSystem.setProject_id(
				softmgrSystemService.selectByPrimaryKey(SoftmgrSubSystem.getSystem_id()).getProject_id());
		SoftmgrSubSystem.setHiddenSystemId(String.valueOf(SoftmgrSubSystem.getSystem_id()));
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(SoftmgrSubSystem);
		return responseObject;
	}

	/**
	 * 删除子系统
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftSubSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSoftSubSystem(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSubSystem SoftmgrSubSystem = softmgrSubSystemService.selectByPrimaryKey(id);
		SoftmgrSubSystem.setDeleted(DeleteStatus.TRUE);
		softmgrSubSystemService.updateByPrimaryKey(SoftmgrSubSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 获取所有子系统
	 * 
	 * @return
	 */
	@RequestMapping(value = "getSoftSubSystem", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftSubSystem() throws Exception {

		SoftmgrSubSystemExample example = new SoftmgrSubSystemExample();
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);

		List<SoftmgrSubSystem> list = softmgrSubSystemService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrSubSystem subSystem : list) {
			JSONObject json = new JSONObject();
			json.put("id", subSystem.getId());
			json.put("text", subSystem.getSub_system_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	/**
	 * 判断是否存在子系统
	 * 
	 * @param systemId
	 * @param subSystemName
	 * @return
	 */
	private boolean exitSoftmgrSubSystem(String systemId, String subSystemName) {
		SoftmgrSubSystemExample example = new SoftmgrSubSystemExample();
		example.getCriteria().andSystem_idEqualTo(Integer.valueOf(systemId));
		example.getCriteria().andSub_system_nameEqualTo(subSystemName);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrSubSystem> SoftmgrSubSystems = softmgrSubSystemService.selectByExample(example);
		if (SoftmgrSubSystems != null && SoftmgrSubSystems.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据项目ID获取系统
	 * 
	 * @param projectId
	 * @return
	 */
	private List<SoftmgrSystem> getSoftmgrSystem(Integer projectId) {
		SoftmgrSystemExample listSystemExample = new SoftmgrSystemExample();
		listSystemExample.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		listSystemExample.getCriteria().andProject_idEqualTo(projectId);
		return softmgrSystemService.selectByExample(listSystemExample);
	}

	/**
	 * 根据系统id跟子系统名称获取子系统列表
	 * 
	 * @param systemId
	 * @param subSystemName
	 * @return
	 */
	private List<SoftmgrSubSystem> getSoftmgrSubSystem(String systemId, String searchSubSystemName, Integer page,
			Integer rows) {
		SoftmgrSubSystemExample listSubSystemExample = new SoftmgrSubSystemExample();
		listSubSystemExample.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		if (StringUtil.isNotEmpty(systemId)) {
			listSubSystemExample.getCriteria().andSystem_idEqualTo(Integer.valueOf(systemId));
		}
		if (!StringUtil.isEmpty(searchSubSystemName)) {
			listSubSystemExample.getCriteria().andSub_system_nameLike("%" + searchSubSystemName + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}
		listSubSystemExample.setCount(page * rows);
		listSubSystemExample.setLimitStart((page - 1) * rows);
		return softmgrSubSystemService.selectByExample(listSubSystemExample);
	}

	/**
	 * 根据系统id跟子系统名称获取子系统列表
	 * 
	 * @param systemId
	 * @param subSystemName
	 * @return
	 */
	private List<SoftmgrSubSystem> getSoftmgrSubSystem(List<SoftmgrSystem> listSystem, String searchSubSystemName,
			Integer page, Integer rows) {
		List<Integer> ids = new ArrayList<Integer>();
		for (SoftmgrSystem softmgrSystem : listSystem) {
			ids.add(softmgrSystem.getId());
		}
		SoftmgrSubSystemExample listSubSystemExample = new SoftmgrSubSystemExample();
		listSubSystemExample.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		listSubSystemExample.getCriteria().andSystem_idIn(ids);
		if (!StringUtil.isEmpty(searchSubSystemName)) {
			listSubSystemExample.getCriteria().andSub_system_nameLike("%" + searchSubSystemName + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}
		listSubSystemExample.setCount(page * rows);
		listSubSystemExample.setLimitStart((page - 1) * rows);
		return softmgrSubSystemService.selectByExample(listSubSystemExample);
	}

}
